function urlToImage(url,fn){
    var img = new Image();
    img.src = url;
    img.onload = function () {
        fn(img);
    }
}
function imageToCanvas(image){
    var cvs = document.createElement("canvas");
    var ctx = cvs.getContext('2d');
    cvs.width = image.width;
    cvs.height = image.height;
    ctx.drawImage(image, 0, 0, cvs.width, cvs.height);
    return cvs ;
}
function canvasResizeToFile(canvas,quality,fn){
    canvas.toBlob(function(blob) {
        fn(blob);
    }, 'image/jpeg', quality);
}
function fileToDataURL(file,fn){
    var reader = new FileReader();
    reader.onloadend = function(e){
        fn(e.target.result);
    }
    reader.readAsDataURL(file);
}
function dataURLToImage(data_url,fn){
    var img = new Image();
    img.onload = function() {
        fn(img);
    }
    img.src = data_url;
}
function dataURLToFile(data_url) {
    var arr = data_url.split(','), mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
    while(n--){
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], {type:mime});
}
function imgCompress(file,quality,callback){
    fileToDataURL(file, function (data_url) {
        dataURLToImage(data_url, function (image) {
            canvasResizeToFile(imageToCanvas(image), quality, callback);
        });
    });
}
